#ifndef _IR_FUNCTION_H_
#define _IR_FUNCTION_H_

#include "Global.h"

#define P_PWR_IRTX      _pa3
#define P_PWR_IRRX      _pa6


#define IR_TXLED_ON()   do{P_PWR_IRTX = 1; _isgen = 1; }while(0)
#define IR_TXLED_OFF()  do{_isgen = 0;}while(0)
#define CMP_ON()        do{_opdsw23 = 1; _opdcen = 1;}while(0)
#define CMP_OFF()       do{_opdcen = 0; _opdsw23 = 0;}while(0)

// #define IR_RXLED_ON()   do{_opdsw6 = 1; }while(0)
// #define IR_RXLED_ON()   do{P_PWR_IRRX = 1; _opda0en = 1; _opda1en = 1; _opdsw6 = 1;}while(0)
// #define IR_RXLED_OFF()  do{P_PWR_IRRX = 0; _opda0en = 0; _opda1en = 0; _opdsw6 = 0;}while(0)
#define IR_RXLED_ON()   do{P_PWR_IRRX = 1; _opda0en = 1; _opda1en = 1; }while(0)
#define IR_RXLED_OFF()  do{P_PWR_IRRX = 0; _opda0en = 0; _opda1en = 0; }while(0)

// #define C_ISINK_10MA    ((10-10)/6)
// #define C_ISINK_16MA    ((16-10)/6)
// #define C_ISINK_22MA    ((22-10)/6)
// #define C_ISINK_28MA    ((28-10)/6)
// #define C_ISINK_34MA    ((34-10)/6)
// #define C_ISINK_40MA    ((40-10)/6)
// #define C_ISINK_46MA    ((46-10)/6)
// #define C_ISINK_52MA    ((52-10)/6)
// #define C_ISINK_58MA    ((58-10)/6)
// #define C_ISINK_64MA    ((64-10)/6)
// #define C_ISINK_100MA   ((100-10)/6)
enum
{
    C_ISINK_10MA    = 0,
    C_ISINK_16MA,
    C_ISINK_22MA,
    C_ISINK_28MA,
    C_ISINK_34MA,
    C_ISINK_40MA,
    C_ISINK_46MA,
    C_ISINK_52MA,
    C_ISINK_58MA,
    C_ISINK_64MA,
    C_ISINK_70MA,
    C_ISINK_76MA,
    C_ISINK_82MA,
    C_ISINK_88MA,
    C_ISINK_94MA,
    C_ISINK_100MA,
    C_ISINK_106MA,
    C_ISINK_112MA,
    C_ISINK_118MA,
    C_ISINK_124MA,
    C_ISINK_130MA,
    C_ISINK_136MA,
    C_ISINK_142MA,
    C_ISINK_148MA,
    C_ISINK_154MA,
    C_ISINK_160MA,
    C_ISINK_166MA,
    C_ISINK_172MA,
    C_ISINK_178MA,
    C_ISINK_184MA,
    C_ISINK_190MA,
    C_ISINK_196MA,
    C_ISINK_202MA,
};

#define C_NORMAL_DISTANCE  0
#define C_LONG_DISTANCE    1

#define C_ISINK_NORMAL          C_ISINK_16MA
#define C_ISINK_LONG            C_ISINK_82MA

#define C_DEFAULT_THRESHOLD     50
#define C_DEFAULT_THRESHOLD_OFF 40

#define C_DBC_TIMES        6

#define _CARRY_DET_TIME_450US_  (0)
#define _CARRY_DET_TIME_225US_  (1)

#if _CARRY_DET_TIME_450US_
#define C_TM_DET_CARRY          (225/2)
#define C_TM_IR_HEAD_H          (444)
#define C_TM_IR_HEAD_L_MIN      (444)      
#define C_TM_IR_HEAD_L_MAX      (666)
#define C_TM_IR_DATA_SYNC_MIN   (3)
#define C_TM_IR_DATA_SYNC_MAX   (6+1)
#define C_TM_IR_DATA_MIN        (3)
#define C_TM_IR_DATA_MAX        (16)
#define C_TM_IR_DATA_0_1        (9)
#else
#define C_TM_DET_CARRY          (225)
#define C_TM_IR_HEAD_H          (888)
#define C_TM_IR_HEAD_L_MIN      (888)      
#define C_TM_IR_HEAD_L_MAX      (1332)
#define C_TM_IR_DATA_SYNC_MIN   (6)
#define C_TM_IR_DATA_SYNC_MAX   (12+1)
#define C_TM_IR_DATA_MIN        (6)
#define C_TM_IR_DATA_MAX        (32)
#define C_TM_IR_DATA_0_1        (18)
#endif

#define C_MAX_IDX_SENS  26


#define C_MAX_IR_VALUE_NORMAL   310
#define C_MIN_IR_VALUE_NORMAL   5
#define C_MAX_IDX_SENS_NORMAL   C_MAX_IDX_SENS


#define C_MAX_IR_VALUE_LONG     900
#define C_MIN_IR_VALUE_LONG     100
#define C_MAX_IDX_SENS_LONG     C_MAX_IDX_SENS

extern uint16 Diff_IR;
extern bit F_IR_Active;
extern uint8  IR_RemoteCmd;
// extern uint8 IR_PowerSel;
// extern uint8 IR_Gain_Sel;
extern uint16 IR_Threshold;
extern uint16 IR_Threshold_Off;
extern uint8 IR_SensIdx;
extern uint8 IR_DistanceMode;

void    ISinkCurrentSet(u8 current_set);
void    init_IRFunction(void);
// uint8   IR_Detect(void);
// uint8   IR_Detect(uint8 current,uint8 long_or_short,uint16 threshold);

// uint8   IR_Detect(uint8 current,uint8 long_or_short,uint16 threshold_on, uint16 threshold_off);
// uint8   IR_Detect(uint8 long_or_short,uint16 threshold_on, uint16 threshold_off);
uint8   IR_Detect(void);
uint8 IR_RemoteDetect(void);
void    IR_UpdateSensitivity(void);
void    IR_DistanceSetting(uint8 op);
// void    IR_StudyDistance(void);
uint8    IR_StudyDistance(void);
#endif